/**
 * Created by weng on 2017/8/16.
 */

var data_temp = {};
var  temp_page_size= 15;

/**
 * 获取在院名单
 * @param info
 */
var homeAccountList = function () {
    console.log("获取信息");
    var obj = {};
    var phone = $("#phone").val();
    if(phone)
        obj.phone = phone;
    $.ajax({
        url:'/hospital/getAccountList',
        type:'post',
        dataType:'json',
        data:obj,
        success:function(data) {
            if (data.code != 200) {
                alert("查询错误");
                return;
            } else {
                if(data.info.length > 0){
                    data_temp = data.info
                    var temp_teble_info = data_temp.slice(0 , temp_page_size);
                    createPageMin(temp_page_size, 5, Math.ceil(data_temp.length), data_temp);
                    showTableInfo(temp_teble_info);
                }

            }
        }
    });
};



function createPageMin(pageSize, buttons, total,data_temp) {
    $("#pagination").empty();
    $('#pagination').Paging({pagesize:pageSize,count:total,toolbar:true,pageSizeList:[temp_page_size],
        callback:function(page,size,count){
            page = page - 1;
            var temp_teble_info = data_temp.slice(page  * size ,page * pageSize + size);
            showTableInfo(temp_teble_info);
        }});
}


function showTableInfo(data_arr){
    var html = "";
    var thead = $('#taccountList thead');
    var tbody = $('#taccountList tbody');
    tbody.empty();
    thead.empty();
    var htmlHead = "<tr>"
        + "<th>住院号：</th>"
        + "<th>姓名：</th>"
        + "<th>入住房号：</th>"
        + "<th>性别：</th>"
        + "<th>电话：</th>"
        + "<th>生日：</th>"
        + "<th>操作：</th>"
        + "</tr>"
    htmlHead = $(htmlHead);
    thead.append(htmlHead);

    for(var i in data_arr){
        html += '<tr>' +
            '<th  id="id'+ data_arr[i].id +'">'+  data_arr[i].hospitalId + '</th>' +
            '<th  id="name'+ data_arr[i].id +'">'+  data_arr[i].name + '</th>' +
            '<th  id="roomId'+ data_arr[i].id +'">'+  data_arr[i].roomId + '</th>' +
            '<th  id="sex'+ data_arr[i].id +'">'+  data_arr[i].sex + '</th>' +
            '<th  id="phone'+ data_arr[i].id +'">'+  data_arr[i].phone + '</th>' +
            '<th  id="birthday'+ data_arr[i].id +'">'+  data_arr[i].birthday + '</th>' +
            "<th>" +
            "<a onclick='changeToDetail("+ data_arr[i].id +")'>详情</a>" +
            '</th>' +
            '</tr>'
    }


    var tr = $(html);
    tbody.append(tr);
}

var changeToDetail = function (id) {
    console.log("id is:",id);
    layer.open({
            type: 2,
            title: '',
            //area: ['1000px', 'auto'],
            maxmin: true,
            //width:'200px',
            area: ['1266px', '720px'],
            content: '/boss/taccountInfo?id=' + id,
            end: function(){
               // $.layer.tips('Hi', '#about', {tips: 1})
            }
        });
}

var updatePhone = function (id) {
    var obj = {};
    obj.phone = $("#phone"+ id).val();
    obj.id =id;

    $.ajax({
        url:'/hospital/updatePhone',
        type:'post',
        dataType:'json',
        data:obj,
        success:function(data){
            if(200 === data.code) {
                location.reload();
            }
            alert(data.info)
        }
    });
};

var addUser = function () {
    var obj = {};
    obj.id = $("#id").val();
    obj.name = $("#name").val();
    obj.sex = $("#sex").val();
    obj.phone = $("#phone").val();
    obj.birthday = $("#birthday").val();

    if(!obj.id){
        return  layer.msg('住院号必须填写');
    }else if(!obj.name ){
        return  layer.msg('姓名必须填写');
    }else if(!obj.sex ){
        return  layer.msg('性别必须填写');
    }else if(!obj.phone ){
        return  layer.msg('电话号码必须填写');
    }else if(obj.phone.length > 11 ){
        return  layer.msg('电话号码长度不可超过11位');
    }
	else if(!obj.birthday ){
		obj.birthday = "";
        //return  layer.msg('出生年月必须填写');
    }
    $.ajax({
        url:'/hospital/saveAccount',
        type:'post',
        dataType:'json',
        data:obj,
        success:function(data){
            alert(data.info)
            if(200 === data.code) {
                history.go(-1)
            }
        }
    });
};





var updateTaccount = function () {
    layer.msg('确认修改信息？', {
        time: 0 //不自动关闭
        ,btn: ['确定', '取消']
        ,yes: function(index){
            var obj = {};
            obj.id = $("#id").val();

            obj.hospitalId = $("#hospitalId").val();
            obj.roomId = $("#roomId").val();
            obj.sex = $("#sex").val();
            obj.phone = $("#phone").val();
            obj.birthday = $("#birthday").val();
            obj.IdCard = $("#IdCard").val();
            obj.history = $("#history").val();
            obj.allergy = $("#allergy").val();
            obj.level = $("#level").val();
            obj.total = $("#total").val();
            obj.payInfo = $("#payInfo").val();
            obj.vacate = $("#vacate").val();
            obj.call = $("#call").val();
            obj.risk = $("#risk").val();
            obj.food = $("#food").val();
            obj.feed = $("#feed").val();
            obj.festival = $("#festival").val();
            obj.extraMeal = $("#extraMeal").val();
            obj.WEpay = $("#WEpay").val();
            obj.electric = $("#electric").val();
            obj.bed = $("#bed").val();
            obj.consumable = $("#consumable").val();
            obj.standbyMoney = $("#standbyMoney").val();
            obj.address = $("#address").val();
            obj.unit = $("#unit").val();
            obj.intoRoomTime = $("#intoRoomTime").val();
            obj.outRoomTime = $("#outRoomTime").val();
            obj.extendInfo = $("#extendInfo").val();


            obj.name1 = $("#name1").val();
            obj.name2 = $("#name2").val();
            obj.name3 = $("#name3").val();

            obj.relation1 = $("#relation1").val();
            obj.relation2 = $("#relation2").val();
            obj.relation3 = $("#relation3").val();
            obj.phone1 = $("#phone1").val();
            obj.phone2 = $("#phone2").val();
            obj.phone3 = $("#phone3").val();
            obj.uid3 = $("#uid3").val();
            obj.uid2 = $("#uid2").val();
            obj.uid1 = $("#uid1").val();

            obj.wechat1 = $("#wechat1").val();
            obj.wechat2 = $("#wechat2").val();
            obj.wechat3 = $("#wechat3").val();

            obj.reuint1 = $("#reuint1").val();
            obj.reuint2 = $("#reuint2").val();
            obj.reuint3 = $("#reuint3").val();

            console.log("obj is :",obj);



            var formData = new FormData();
            formData.append('files',$("#btn_file")[0].files[0]);
            formData.append('obj',JSON.stringify(obj));

            var url="/hospital/updateTaccount";
            $.ajax({
                url:url,
                type: 'POST',
                cache: false,
                data: formData,
                processData: false,
                contentType: false,
                success:function(data){

                    layer.close(index);
                    if(200 === data.code) {
                        layer.msg('修改成功', {
                            icon: 1,
                            time: 1000 //2秒关闭（如果不配置，默认是3秒）
                        }, function(){
                            location.reload();
                        });
                    }else {
                        layer.msg(data.info );
                    }
                }
            });
        }
    });
}


var showTaccountDetail = function (flag) {
    console.log("显示记录",flag);
    var id = $("#id").val();
    console.log("id is",id);

    var url="/hospital/showTaccountDetail";
    $.ajax({
        url:url,
        type: 'POST',
        cache: false,
        async:false,
        data: {id:id,flag:flag},
        success:function(data){
            console.log(data)
            if(200 === data.code) {
                var data_arr = data.info;

                var htmlHead = "<tr>"
                    + "<th>时间：</th>"
                    + "<th>内容：</th>"
                    + "</tr>";

                var html = "";
                for(var i in data_arr){
                    html += '<tr>'
                    html +=  '<td >'+ data_arr[i].time +'</td>'
                    html +=  '<td >'+ data_arr[i].info +'</td>'
                    html += '</tr>';
                }

                layer.open({
                    type: 1,
                    skin: 'layui-layer-rim', //加上边框
                    title:"详细记录", //样式类名
                    // closeBtn: 0, //不显示关闭按钮
                    area: ['800px', '400px'],
                    // shadeClose: true, //开启遮罩关闭
                    content: "<table class='table table-bordered'>"
                    + "<thead>"+htmlHead+"</thead>"
                    + "<tbody>"+html+"</tbody>"
                    + "</table>"
                });
            }else {
                layer.msg(data.info );
            }
        }
    });
}

var addTaccountDetail = function (flag) {
    var id = $("#id").val();
    console.log("id is",id);
    layer.open({
        type: 2,
        title: '',
        //area: ['1000px', 'auto'],
        maxmin: true,
        //width:'200px',
        area: ['800px', '620px'],
        content: '/boss/AddTaccountDetail?id=' + id + "&flag="+flag,
        end: function(){
            // $.layer.tips('Hi', '#about', {tips: 1})
        }
    });
}
var readControle = function () {

    if(!$('#hospitalId').attr("readonly")){
        $('input').attr("readonly","readonly");
        $('#sub').attr("disabled","disabled");

        $('input').addClass('disableClass');
        $('.input-group-addon').addClass('disableClass');

        $("input").css("background-color","black");

        $(".input-group .btn-primary").css("display","none");
        $(".input-group .btn-success").css("display","block");
       //  $("input").css("border","none");
       //  $("input").css("color","white");

        $('#readControle').text("允许更改");
        $("#sub").css('display','none');
    }else {
        $('#sub').removeAttr("disabled");
        $('input').removeAttr("readonly");
        $("#sub").css('display','block');

        $('input').removeClass('disableClass');
        $('.input-group-addon').removeClass('disableClass');
        $("input.form-control").css("background-color","#eee");

        $(".input-group .btn-primary").css("display","block");
        $(".input-group .btn-success").css("display","none");

        $('#readControle').text("禁止更改");
    }
}


var addDetail = function () {
    var id = $("#id").val();
    var type = $("#type").val();
    var time = $("#time").val();
    var info = $("#info").val();
    var url="/hospital/addDetail";
    var obj =  {};
    obj.id = id;
    obj.type = type
    obj.time = time
    obj.info = info

    $.ajax({
        url:url,
        type: 'POST',
        cache: false,
        data: obj,
        //processData: false,
        //contentType: false,
        success:function(data){
            console.log("333333333333333",data);
            if(200 === data.code) {
                layer.msg('修改成功', {
                    icon: 1,
                    time: 1000 //2秒关闭（如果不配置，默认是3秒）
                }, function(){
                    location.reload();
                });
            }else {
                layer.msg(data.info );
            }
        }
    });
}